using System;
using UnityEngine;
using System.Collections;

public class SAPlayerMove
{
	public SAPlayerMove ()
	{

	}



	public SAMapPos mapPos = new SAMapPos();
	public SAMapGrid movePos = new SAMapGrid();
	public int[] movePath = null;

	public bool movePathBuffer = false;
	public int movePathCount = 0;
	public int movePathPos = 0;
	public SAMapGrid movePathBackup = new SAMapGrid();
	
	public Boolean isDirectionBuffer = false;
	public int directionBuffer = (int)SAMapDirectionType.COUNT;
	public int moveDirection = (int)SAMapDirectionType.SOUTHWEST;

	public SAPlayer owner = null;

	private SAMapGrid mapGridLast = new SAMapGrid();
	public bool isMoving = false;


	public void stopMoving()
	{
		isMoving = false;

		mapPos.mapGrid.setValue( movePos.posX , movePos.posY );

		owner.movePosReal();
		
		movePathPos = 0;
		movePathCount = 0;
		
		if ( isDirectionBuffer ) 
		{
			isDirectionBuffer = false;
			moveDirection = directionBuffer;
		}
		
		movePathBuffer = false;
		
		owner.setAction( (int)SAActionType.STAND , moveDirection );
	}


	public void endMoving()
	{
		isMoving = false;

		if ( movePathCount > movePathPos )
		{
			int x = SAMapManager.instance.getXWithIndex( movePath[ movePathCount - 1 ] );
			int y = SAMapManager.instance.getYWithIndex( movePath[ movePathCount - 1 ] );

			mapPos.mapGrid.setValue( x , y );
		}
		else
		{
			mapPos.mapGrid.setValue( movePos.posX , movePos.posY );
		}

		//Debug.Log( "endMoving" + movePos.posX + " " + movePos.posY );

		owner.movePosReal();

		movePathPos = 0;
		movePathCount = 0;

		if ( isDirectionBuffer ) 
		{
			isDirectionBuffer = false;
			moveDirection = directionBuffer;
		}
		
		movePathBuffer = false;
				
		owner.setAction( (int)SAActionType.STAND , moveDirection );
	}

	public void startMove( int[] b )
	{
		SAMapManager.instance.pathFinder.uncompressePath( b );
		ArrayList list = SAMapManager.instance.pathFinder.PathCache;

		if ( isMoving )
		{
			int index = 0;
			for ( int i = 0 ; i < movePath.Length ; i++ )
			{
				if ( movePath[ i ] == (int)list[ 0 ] )
				{
					index = 0;
				}
			}

			if ( index > 0 )
			{
				int len = index + (int)list.Count;

				int[] buff = new int[ movePath.Length ];
				movePath.CopyTo( buff , 0 );
				movePath = new int[ len ];

				for ( int i = 0 ; i < index ; i++ )
				{
					movePath[ i ] = buff[ i ];
				}

				list.CopyTo( movePath , index );

				return;
			}
		}

		movePath = new int[ list.Count ];
		movePathCount = movePath.Length;
		list.CopyTo( movePath );

		int x = SAMapManager.instance.getXWithIndex( movePath[ 0 ] );
		int y = SAMapManager.instance.getYWithIndex( movePath[ 0 ] );
		mapPos.mapGrid.setValue( x , y );

		isMoving = true;
		movePathPos = 1;
		owner.setAction( (int)SAActionType.MOVE , moveDirection );
		updateDirection();
	}
	
	
	public void startMove( int x , int y )
	{
		if ( mapPos.mapGrid.posX == x && mapPos.mapGrid.posY == y ) 
		{
			return;
		}

		//Debug.Log( "startMove" + mapPos.mapGrid.posX + " " + mapPos.mapGrid.posY + x + " " + y );

		movePathCount = SAMapManager.instance.pathFinder.findPath( out movePath , mapPos.mapGrid.posX , mapPos.mapGrid.posY , x , y );

		if ( movePathCount >= 2 ) 
		{
			movePathPos = 1;
			isMoving = true;
			sendMoveMsg();
			owner.setAction( (int)SAActionType.MOVE , moveDirection );
			updateDirection();
		}

	}
	
	
	private void updateDirection()
	{
		if ( movePathBuffer )
		{
			if ( mapPos.mapGrid.checkPos( movePathBackup ) )
			{
				endMoving();
				return;
			}

			int[] pathBuffer = null;
			int pathBufferCount = SAMapManager.instance.pathFinder.findPath( out pathBuffer , mapPos.mapGrid.posX , mapPos.mapGrid.posY , movePathBackup.posX , movePathBackup.posY );
			movePathBuffer = false;

			if ( pathBufferCount >= 2 ) 
			{
				movePathCount = pathBufferCount;
				movePath = pathBuffer;

				sendMoveMsg();
				movePathPos = 1;
			}
			else
			{
				return;
			}
			
			//var start:Boolean = false;
			
			//if ( owner.team && owner.team.leader == owner )
			//{
			//	for ( var i:int = 1 ; i < owner.team.members.length ; i++ )
			//	{
			//		owner.team.members[i].moveHandler.movePath = owner.team.members[i-1].moveHandler.movePath.concat();
					
					//if ( owner.team.members[i].moveHandler.movePath.length && owner.team.members[i].moveHandler.movePath[0] != owner.team.members[i-1].moveHandler.movePath[0] )
					//{
					//	
					//	owner.team.members[i].moveHandler.movePath.unshift( owner.team.members[i-1].moveHandler.movePos.clone() );
					//}
					
			//		owner.team.members[i].moveHandler.movePath.pop();
			//		owner.team.members[i].moveHandler.movePath.unshift( owner.team.members[i-1].moveHandler.mapPos.mapGrid.clone() );
					
			//		if ( owner.team.members[i].state != GameCeatureState.MOVE )
			//		{
			//			owner.team.members[i].moveHandler.startMove();
			//		}
					
					
					//trace( moveHandler.movePath );
					//trace( team.members[i].moveHandler.movePath );
			//	}
			//}
		}
			
		if ( movePathPos == movePathCount )
		{
			endMoving();
			return;
		}

		int index = movePath[ movePathPos ];
		movePos.setValue ( SAMapManager.instance.getXWithIndex( index ) , SAMapManager.instance.getYWithIndex( index ) );
		movePathPos++;

		int lastDirection = moveDirection;
		moveDirection = SAMapDirection.GetDirection( movePos , mapPos.mapGrid );

		if ( moveDirection == (int)SAMapDirectionType.COUNT )
		{
			moveDirection = lastDirection;
		}

		owner.setDirection( (int)moveDirection );

		mapPos.startMove( movePos.posX , movePos.posY );
		
		if ( lastDirection != moveDirection ) 
		{
			owner.setAction( (int)SAActionType.MOVE , moveDirection );
		}
	}
	
	public void updateMove( float delay )
	{
		if ( !isMoving )
		{
			return;
		}
		
		mapPos.updateMove( delay );
		
		owner.movePosReal();

		SAMapManager.instance.updateMainPlayer( owner );
		
		if ( !mapPos.isMoving )
		{
			updateDirection();
		}
		
		if ( !mapPos.mapGrid.checkPos( mapGridLast ) )
		{
			//GameCreatureManager.instance.updatePlayerScene( owner as GamePlayer );
		}
		
		mapGridLast.posX = mapPos.mapGrid.posX;
		mapGridLast.posY = mapPos.mapGrid.posY;
	}


	public unsafe void sendMoveMsg()
	{
		if ( movePathCount < 2 )
		{
			return;
		}

		if ( movePathCount > GameDefine.MAX_MOVE_PATH )
			movePathCount = GameDefine.MAX_MOVE_PATH;
		
		if ( SAMapManager.instance.pathFinder.compressePath( movePath ) ) 
		{
			ArrayList path = SAMapManager.instance.pathFinder.PathCache;

			GameNetMessage.SEND_RECV_MSG_MOVE msg = new GameNetMessage.SEND_RECV_MSG_MOVE();
			msg.initNetHead();

			msg.GUID = 0;
			msg.MapID = SAMapManager.instance.mapID;
			msg.Count = path.Count;

			for ( int i = 0; i < path.Count ; i++ ) 
			{
				msg.Path[ i ] = (int)path[ i ];
			}

			GameSocketManager.instance.sendMsg( msg );
		}

	}
}








